package com.cj.zhuzhuangtudemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private BarChart barChart;
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        barChart= (BarChart) findViewById(R.id.barChart);
        btn= (Button) findViewById(R.id.btn);
        btn.setOnClickListener(this);
        barChart.setDescription("");
        //绘制阴影
        barChart.setDrawBarShadow(false);
        //设置数值在柱状条上面
        barChart.setDrawValueAboveBar(true);
        //设置柱状条边框
        barChart.setDrawBorders(true);
//        设置没有数据的时候什么表现
        barChart.setNoDataText("暂无数据text");
        //设置没有数据的时候的描述
        barChart.setNoDataTextDescription("暂无数据descrip");
        //获取x轴
        XAxis xAxis=barChart.getXAxis();
        //x轴的竖直线要不要
        xAxis.setDrawGridLines(true);
        //x轴在上面还是下面
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //左Y轴的横线要
        barChart.getAxisLeft().setDrawGridLines(false);
        //右Y轴的横线不要
        barChart.getAxisRight().setDrawLabels(false);
        //右Y轴不显示
//        barChart.getAxisRight().setEnabled(false);
        YAxis yAxis=barChart.getAxisLeft();
        yAxis.setLabelCount(5,true);
        //右Y轴的标题不要
        barChart.getAxisRight().setDrawLabels(false);
        //表示某个类型的柱状条的图标在哪个位置，此为显示在上方中间
        barChart.getLegend().setPosition(Legend.LegendPosition.ABOVE_CHART_CENTER);
    }

    @Override
    public void onClick(View v) {
        //每个柱状条的y值和第几个
        BarEntry entry=new BarEntry(10f,0);
        BarEntry entry1=new BarEntry(1f,1);
        BarEntry entry2=new BarEntry(2f,2);
        //将某个类型的柱状条放一起
        List<BarEntry> lists=new ArrayList<>();
        lists.add(entry);
        lists.add(entry1);
        lists.add(entry2);
        //某个类型的柱状条合并加上Legend图示文字
        BarDataSet set1=new BarDataSet(lists,"1号");
        //string数组表示x轴的下标值
        BarData data=new BarData(new String[]{"1","2","3","4","8"},set1);
        barChart.setData(data);
        barChart.animateXY(2000,2000, Easing.EasingOption.EaseInCubic, Easing.EasingOption.EaseInCubic);
    }
}
